package com.xiaoxi.strategymode.strategy;

import com.xiaoxi.strategymode.play.Hand;

import java.util.Random;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.strategymode.strategy
 * @ClassName ProbStrategy
 * @Description TODO
 * @Date 2023/3/13 21:27
 * @Version 1.0
 */
public class ProbStrategy implements IStrategy {

    private int currentHandValue = 0;
    private int prevHandValue = 0;

    private int[][] winNum = {
            {1, 1, 1},
            {1, 1, 1},
            {1, 1, 1}
    };

    @Override
    public Hand nextHand() {
        int bet = new Random().nextInt(getSum(currentHandValue));
        int handValue;
        if (bet < winNum[currentHandValue][0]) {
            handValue = 0;
        } else if (bet < winNum[currentHandValue][1]) {
            handValue = 1;
        } else {
            handValue = 2;
        }
        prevHandValue = currentHandValue;
        currentHandValue = handValue;
        return Hand.getHandValue(handValue);
    }

    private int getSum(int hv) {
        int sum = 0;
        for (int i = 0; i < 3; i++) {
            sum += winNum[hv][i];
        }
        return sum;
    }

    @Override
    public void study(boolean win) {
        if (win) {
            winNum[prevHandValue][currentHandValue]++;
        } else {
            winNum[prevHandValue][(currentHandValue + 1) % 3]++;
            winNum[prevHandValue][(currentHandValue + 2) % 3]++;
        }
    }
}
